<?php

use Drupal\shusy_banner\Entity\ShusyBanner;
use Drupal\shusy_banner\Entity\ShusyBannerItem;

function shusy_banner_shusy_banner_entity_form($form, &$form_state, ShusyBanner $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_banner') . '/shusy_banner.forms.inc';
  $form_state[ShusyBanner::class] = $e;
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('名称'),
    '#required' => TRUE,
    '#default_value' => $e->name,
  ];


  $form["items"] = [
    '#type' => 'fieldset',
    '#title' => t('BANNERS'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  ];
  $form["items"]['ii'] = [
    '#type' => 'item',
    '#prefix' => '<div id="items-form-wrapper">',
    '#suffix' => '</div>',
    '#theme' => 'shusy_banner_form_items',
  ];
  if (empty($form_state['items'])) {
    $items = shusy_banner_item_load_multiple(FALSE, ['banner_id' => $e->id]);
    if (!empty($items)) {
      $form_state['items'] = $items;
    }
    else {
      $form_state['items'] = [];
    }
  }
  foreach ($form_state['items'] as $i => $item) {
    if ($item instanceof ShusyBannerItem) {
      $form['items']['ii'][$i] = [
        'id' => [
          '#type' => 'value',
          '#value' => $item->id,
        ],
        'image' => [
          '#title' => t('图片'),
          '#type' => 'managed_file',
          '#default_value' => $item->image,
          '#upload_location' => 'public://tmp/',
          '#title_display' => 'invisible',
        ],
        'link' => [
          '#type' => 'select',
          '#title' => t('跳转位置'),
          '#options' => $item->getLinkOptions(),
          '#default_value' => $item->link,
          '#chosen' => TRUE,
          '#title_display' => 'invisible',
        ],
        'weight' => [
          '#type' => 'weight',
          '#title' => t('Weight'),
          '#default_value' => $item->weight,
          '#delta' => 10,
          '#title_display' => 'invisible',
        ],
        'status' => [
          '#title' => t('启用'),
          '#title_display' => 'invisible',
          '#type' => 'checkbox',
          '#default_value' => $item->status,
        ],
      ];
    }
  }
  $form["items"]['add_item'] = [
    '#type' => 'submit',
    '#value' => t('添加BANNER'),
    '#submit' => ['shusy_banner_add_item_submit'],
    '#ajax' => [
      'callback' => 'shusy_banner_add_item_callback',
      'wrapper' => 'items-form-wrapper',
    ],
  ];

  $form['putin'] = [
    '#type' => 'select',
    '#title' => t('放置位置'),
    '#options' => $e->getPutinOptions(),
    '#default_value' => $e->putin,
    '#chosen' => TRUE,
  ];
  $form['status'] = [
    '#type' => 'select',
    '#title' => t('状态'),
    '#options' => $e->getStatusOptions(),
    '#default_value' => $e->status,
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];
  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_banner_add_item_submit($form, &$form_state) {
  $form_state['items'][] = new ShusyBannerItem();
  $form_state['rebuild'] = TRUE;
}

function shusy_banner_add_item_callback($form, &$form_state) {
  return $form['items']['ii'];
}

function theme_shusy_banner_form_items($variables) {
  $output = '';
  $form = $variables['form'];
  $rows = [];
  foreach (element_children($form) as $id) {
    $form[$id]['weight']['#attributes']['class'] = ['item-weight'];
    $rows[] = [
      'data' => [
        drupal_render($form[$id]['image']),
        drupal_render($form[$id]['link']),
        drupal_render($form[$id]['weight']),
        drupal_render($form[$id]['status']),
      ],
      'class' => ['draggable'],
    ];
  }
  $header = [
    t('图片'),
    t('跳转'),
    t('Weight'),
    t('启用'),
  ];

  $table_id = 'items-table';
  $form['items_markup']['markup']['#markup'] = theme('table', [
    'header' => $header,
    'rows' => $rows,
    'attributes' => ['id' => $table_id],
  ]);
  $output .= drupal_render_children($form);
  drupal_add_tabledrag($table_id, 'order', 'sibling', 'item-weight');
  return $output;
}


function shusy_banner_shusy_banner_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyBanner::class];
  if ($e instanceof ShusyBanner) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
    $values = $form_state['values'];
    if (!empty($values['items']['ii'])) {
      $items = $values['items']['ii'];
      foreach ($items as $item) {
        $id = $item['id'];
        if (!empty($id)) {
          $eItem = shusy_banner_item_load($id);
        }
        else {
          $eItem = new ShusyBannerItem();
        }
        $eItem->banner_id = $e->id;
        $eItem->image = $item['image'];
        $eItem->link = $item['link'];
        $eItem->weight = $item['weight'];
        $eItem->status = $item['status'];
        $eItem->save();
      }
    }
  }
}

function shusy_banner_shusy_banner_delete_form($form, &$form_state, ShusyBanner $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_banner') . '/shusy_banner.forms.inc';
  $form_state[ShusyBanner::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_banner/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_banner_shusy_banner_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyBanner::class];
  if ($e instanceof ShusyBanner) {
    $e->delete();
  }
}

function shusy_banner_shusy_banner_item_entity_form($form, &$form_state, ShusyBannerItem $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_banner') . '/shusy_banner.forms.inc';
  $form_state[ShusyBannerItem::class] = $e;

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_banner_shusy_banner_item_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyBannerItem::class];
  if ($e instanceof ShusyBannerItem) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_banner_shusy_banner_item_delete_form($form, &$form_state, ShusyBannerItem $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_banner') . '/shusy_banner.forms.inc';
  $form_state[ShusyBannerItem::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_banner_item/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_banner_shusy_banner_item_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyBannerItem::class];
  if ($e instanceof ShusyBannerItem) {
    $e->delete();
  }
}

